package com.pr.makfilm.user.pages.room.create.design;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 07.10.13
 * Time: 17:46
 * To change this template use File | Settings | File Templates.
 */
public class DesignCell extends Panel
{
   public static final String GREEN_CLASS = "btn btn-sm active btn-success no-border";

   public static final String RED_CLASS = "btn btn-sm active btn-danger no-border";

   public static final String EMPTY_CLASS = "btn btn-sm active btn-white no-border";

   public IModel<String> cssModel;

   private AjaxLink cellButton;

   private final int rowId;

   private final int columnId;

   public DesignCell(String id, int rowId, int columnId)
   {
      super(id);
      this.rowId = rowId;
      this.columnId = columnId;
      cssModel = Model.of(GREEN_CLASS);
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      add(cellButton = new AjaxLink<Void>("cell")
      {
         @Override
         protected void onComponentTag(ComponentTag tag)
         {
            super.onComponentTag(tag);
            tag.put("class", cssModel.getObject());
         }

         @Override
         public void onClick(AjaxRequestTarget target)
         {
            String css = cssModel.getObject();
            if (GREEN_CLASS.equals(css))
            {
               cssModel.setObject(EMPTY_CLASS);
            }
            else if (EMPTY_CLASS.equals(css))
            {
               cssModel.setObject(RED_CLASS);
            }
            else if (RED_CLASS.equals(css))
            {
               cssModel.setObject(GREEN_CLASS);
            }
            target.add(this);
         }

      });
//      cellButton.setOutputMarkupId(true);
   }

   @Override
   protected void onDetach()
   {
      super.onDetach();
      cellButton.detach();
   }

   public String getCssClass()
   {
      return cssModel.getObject();
   }

   public int getRowId()
   {
      return rowId;
   }

   public int getColumnId()
   {
      return columnId;
   }
}
